//
//  TKIMRefreshFooterView.m
//  TKIM_首创
//
//  Created by macpro on 15/12/14.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMRefreshFooterView.h"
#import "TKIM.h"

//加载更多的视图默认高度
#define kRefreshFooterViewHeight 40

//刷新的动画时间
#define kAnimationDuration 0.3

@interface TKIMRefreshFooterView ()
{
    UIScrollView *_scrollView;
    //加载更多回调
    RefreshFooterViewBeginLoadMoreBlock _refreshFooterViewBeginLoadMoreBlock;
}

/**
 *  加载提示
 */
@property (nonatomic, weak) UIActivityIndicatorView *indicatorView;

/**
 *  提示文字
 */
@property (nonatomic, weak) UILabel *tipLabel;



@end

@implementation TKIMRefreshFooterView

#pragma mark - 懒加载创建控件

- (UIActivityIndicatorView *)indicatorView
{
    if (!_indicatorView)
    {
        UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] init];
        [self addSubview:act];
        act.color = [UIColor grayColor];
        _indicatorView = act;
    }
    
    return _indicatorView;
}

- (UILabel *)tipLabel
{
    if (!_tipLabel)
    {
        UILabel *lb = [[UILabel alloc] init];
        lb.textAlignment = NSTextAlignmentCenter;
        lb.text = @"";
        lb.textColor = [UIColor grayColor];
        lb.font = [UIFont systemFontOfSize:13.0f];
        CGSize size =[self sizeWithString:@"正在加载..." Font:lb.font maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        lb.size = size;
        [self addSubview:lb];
        
        _tipLabel = lb;
    }
    
    return _tipLabel;
}

-(UILabel *)textLabel
{
    if (!_textLabel) {
        UILabel * lb = [[UILabel alloc] init];
        _textLabel = lb;
        lb.textColor = [UIColor grayColor];
        lb.font = [UIFont systemFontOfSize:13.0f];
        lb.text = @"";
        lb.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_textLabel];
    }
    return _textLabel;
}

#pragma mark - init方法

- (instancetype)initWithScrollView:(UIScrollView *)scrollView beginLoadMoreBlock:(RefreshFooterViewBeginLoadMoreBlock)block
{
    if (self = [super initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, kRefreshFooterViewHeight)])
    {
        _scrollView = scrollView;
        _refreshFooterViewBeginLoadMoreBlock = block;
        //把当前视图加到uiscrollView
        [_scrollView addSubview:self];
        
        [self indicatorView];
        [self tipLabel];
        [self textLabel];
        
        //注册观察者
        [_scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
        [_scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    }
    
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //contentSize发生变化
    if ([keyPath isEqualToString:@"contentSize"])
    {
        //修改加载更多的y坐标
        self.top = _scrollView.contentSize.height;
    }
    //contentOffset发生变化
    else if ([keyPath isEqualToString:@"contentOffset"])
    {
        /*
         1.判断滚动正在被拖动
         */
        if (_scrollView.isDragging)
        {
            //scrollView的可见高度
            CGFloat visiableH = 0;
            if (_scrollView.height>_scrollView.contentSize.height) {
                visiableH = _scrollView.contentSize.height;
            }else{
                visiableH = _scrollView.height;
            }
            
            //上拉
            if (_scrollView.contentOffset.y + visiableH > _scrollView.contentSize.height && !self.indicatorView.isAnimating)
            {
                if (self.haveMoreData) {
                    //开始加载
                    [self.indicatorView startAnimating];
                    //2.加载更多回调
                    if (_refreshFooterViewBeginLoadMoreBlock)
                    {
                        _refreshFooterViewBeginLoadMoreBlock(self);
                    }
                    
                    self.tipLabel.text = @"正在加载...";
                    self.textLabel.text = @"";
                    _scrollView.contentInset = UIEdgeInsetsMake(0, 0, kRefreshFooterViewHeight, 0);
                }

            }
        }
    }
}

/**
 *  停止刷新
 */
- (void)endRefresh
{
    [UIView animateWithDuration:kAnimationDuration animations:^{
        _scrollView.contentInset = UIEdgeInsetsZero;
    } completion:^(BOOL finished) {
        //4.指示器停止
        [self.indicatorView stopAnimating];
        self.tipLabel.text = @"";

        if (!self.haveMoreData) {
            self.textLabel.text = @"无更多数据";
        }else{
            self.textLabel.text = @"";
        }
    }];
    
//    if (!self.haveMoreData) {
//        [self.indicatorView stopAnimating];
//        self.tipLabel.text = @"";
//        self.textLabel.text = @"无更多数据";
//    }else{
//        [UIView animateWithDuration:kAnimationDuration animations:^{
//            _scrollView.contentInset = UIEdgeInsetsZero;
//        } completion:^(BOOL finished) {
//            [self.indicatorView stopAnimating];
//            self.tipLabel.text = @"";
//            self.textLabel.text = @"";
//        }];
//    }
    
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.tipLabel.frame = CGRectMake((self.width-self.tipLabel.size.width)*0.5+20, 0, self.tipLabel.size.width, self.height);
    self.indicatorView.frame = CGRectMake(self.tipLabel.left-40, 0, self.height, self.height);
    self.textLabel.frame = self.frame;
}

-(CGSize)sizeWithString:(NSString *)textString Font:(UIFont *)font maxSize:(CGSize)maxSize
{
    return  [textString boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

-(void)dealloc
{
    [_scrollView removeObserver:self forKeyPath:@"contentOffset"];
    [_scrollView removeObserver:self forKeyPath:@"contentSize"];
}
@end
